package dhm.com.dhmshop.fragment.wode;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.CollectionListAdapter;
import dhm.com.dhmshop.adapter.mine.CollectionShopAdapter;
import dhm.com.dhmshop.base.BaseFragment;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetCollectionList;
import dhm.com.dhmshop.entity.GetCollectionShop;
import dhm.com.dhmshop.utils.SpUtils;

@SuppressLint("ValidFragment")
public class CollectionFragment extends BaseFragment implements LoginContract.IView {
    @BindView(R.id.recy)
    RecyclerView recy;
    Unbinder unbinder;
    private View rootView;
    private String type;
    private PressenterImpl pressenter;
    private String uid;
    private CollectionListAdapter collectionListAdapter;
    private CollectionShopAdapter collectionShopAdapter;
    private GetCollectionShop getCollectionShop;
    private GetCollectionList getCollectionList;


    //    1.宝贝   2.店铺
    public CollectionFragment(String type) {
        this.type = type;
    }


    @Override
    protected int getLayout() {
        return R.layout.fragment_collecton;
    }

    @Override
    protected void initView() {
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(getActivity(), "uid");
        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        if (type.equals("1")){
            collectionListAdapter = new CollectionListAdapter(getActivity());
            collectionListAdapter.setdeleteClick(new CollectionListAdapter.OnClick() {
                @Override
                public void item(int posttion) {
                    Map<String,String> map=new HashMap<>();
                    map.put("token",Constant.TOKEN);
                    map.put("uid",uid);
                    map.put("cid",getCollectionList.getData().get(posttion).getFavoriteId()+"");
                    pressenter.sendMessage(getActivity(),Constant.DelCollect,map, Bean.class);
                }
            });
            recy.setAdapter(collectionListAdapter);
        }else {
            collectionShopAdapter = new CollectionShopAdapter(getActivity());
            collectionShopAdapter.setdeleteClick(new CollectionShopAdapter.OnClick() {
                @Override
                public void item(int posttion) {
                    Map<String,String> map=new HashMap<>();
                    map.put("token",Constant.TOKEN);
                    map.put("uid",uid);
                    map.put("shop_id",getCollectionShop.getData().get(posttion).getShopId()+"");
                    map.put("cid",getCollectionShop.getData().get(posttion).getFavoriteId()+"");
                    pressenter.sendMessage(getActivity(),Constant.DelShopCollect,map, Bean.class);
                }
            });
            recy.setAdapter(collectionShopAdapter);
        }
    }

    @Override
    protected void initData() {
        if (type.equals("1")) {
            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("uid", uid);
            pressenter.sendMessage(getActivity(), Constant.CollectionList, map, GetCollectionList.class);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("uid", uid);
            pressenter.sendMessage(getActivity(), Constant.MyShopCollects, map, GetCollectionShop.class);

        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.shop_fragment, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }


    public void refresh() {
        if (type.equals("1")) {
            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("uid", uid);
            pressenter.sendMessage(getActivity(), Constant.CollectionList, map, GetCollectionList.class);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("uid", uid);
            pressenter.sendMessage(getActivity(), Constant.MyShopCollects, map, GetCollectionShop.class);

        }
    }



    @Override
    public void requesta(Object data) {
        if (data instanceof GetCollectionList) {
            getCollectionList = (GetCollectionList) data;
            if (getCollectionList.getCode() == 1) {
                collectionListAdapter.setShopList(getCollectionList.getData());
            }else {
                collectionListAdapter.clearShopList();
            }
        } else if (data instanceof GetCollectionShop) {
            getCollectionShop = (GetCollectionShop) data;
            if (getCollectionShop.getCode() == 1) {
                collectionShopAdapter.setShopList(getCollectionShop.getData());
            }else {
                collectionShopAdapter.clearShopList();
            }
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(getActivity(),bean.getMessage() , Toast.LENGTH_SHORT).show();
            refresh();
        }
    }

    @Override
    public void fail(String error) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
